package edu.dayu.demoDubboServer;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.lang.reflect.Method;

public class MyDubboServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("msg="+msg);
        // 客户端在调用服务器 api 时，我们需要定义一个协议
        // 比如我们要求 每次发送的消息都必须以某个字符穿开头 “HelloService#hello#你好 ”
        if (msg.toString().startsWith("HelloService#")){
            String[] split = msg.toString().split("#");
            HelloServiceImpl helloService = new HelloServiceImpl();
            Method method = HelloServiceImpl.class.getMethod(split[1], String.class);
            String result = (String) method.invoke(helloService, split[2]);
            ctx.writeAndFlush(result);
        }
    }
}
